<template>
  <el-autocomplete
    v-model="state1"
    :fetch-suggestions="querySearch"
    clearable
    class="inline-input w-50"
    placeholder="Please Input"
    @select="handleSelect"
  />
</template>

<script setup>
import { onMounted, ref, watch } from 'vue'

// import { storeToRefs } from 'pinia'
// import { useTestStore } from './test.js'

// const testStore = useTestStore()
// const { currentvalue } = storeToRefs(testStore)

const state1 = ref('')

import { defineProps } from 'vue'
const props = defineProps({
  value: {
    // 是否必须属性
    require: true,
    // 属性类型
    type: String,
    // 默认值
    default: 'xxxxx'
  }
})

const emits = defineEmits(['add1', 'decre1'])

watch(
  () => props.value,
  (newVal, oldVal) => {
    console.log('监听基本类型数据testStr')
    state1.value = newVal
    console.log('new', newVal)
    console.log('old', oldVal)
  },
  { immediate: true, deep: true }
)

console.log(props)

const restaurants = ref([])
const querySearch = (queryString, cb) => {
  const results = queryString
    ? restaurants.value.filter(createFilter(queryString))
    : restaurants.value
  // call callback function to return suggestions
  cb(results)
}
const createFilter = (queryString) => {
  return (restaurant) => {
    return restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0
  }
}
const loadAll = () => {
  return [
    { value: 'vue', link: 'https://github.com/vuejs/vue' },
    { value: 'element', link: 'https://github.com/ElemeFE/element' },
    { value: 'cooking', link: 'https://github.com/ElemeFE/cooking' },
    { value: 'mint-ui', link: 'https://github.com/ElemeFE/mint-ui' },
    { value: 'vuex', link: 'https://github.com/vuejs/vuex' },
    { value: 'vue-router', link: 'https://github.com/vuejs/vue-router' },
    { value: 'babel', link: 'https://github.com/babel/babel' }
  ]
}

const handleSelect = (item) => {
  emits('add1', item)
  console.log(item)
}

onMounted(() => {
  restaurants.value = loadAll()
})
</script>
